/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.zeos.bean;

import br.zeos.ZDataSource;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import javax.swing.JEditorPane;
import javax.swing.JTextField;

/**
 *
 * @author hardware
 */
public class ZEditorPane extends JEditorPane implements ActionListener {

    private ZDataSource origem = null;
    private String FieldName = "";

    public ZDataSource getDataSource() {
        return origem;
    }

    public void setDataSource(final ZDataSource origem) {
        this.origem = origem;
        this.origem.addUpdateListener(this);
        this.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
                updateData();
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                updateData();
            }

            @Override
            public void keyReleased(KeyEvent ke) {
                updateData();
            }
        });
    }

    private void updateData() {
        //arrumar
        HashMap<String, String> linha = this.origem.getLine();
        linha.put(FieldName.toUpperCase(), this.getText());
        this.origem.setLinha(linha);
    }

    public String getFieldName() {
        return FieldName;
    }

    public void setFieldName(String FieldName) {
        this.FieldName = FieldName;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        this.setText(origem.getLine().get(this.getFieldName().toUpperCase()));
    }

}
